/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.item;

import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.sample.bean.CheckMenu;
import me.panpf.sketch.sample.bean.CommonItem;
import me.panpf.sketch.sample.bean.InfoMenu;
import me.panpf.sketch.sample.bean.MenuTitle;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.List;

/**
 * SettingsItemProvider
 */
public class SettingsItemProvider extends BaseItemProvider {
    private Context mContext;
    private List<CommonItem> mList;

    /**
     * SettingsItemProvider
     * @param context
     * @param mList
     */
    public SettingsItemProvider(Context context, List<CommonItem> mList) {
        this.mContext = context;
        this.mList = mList;
    }

    /**
     * getList
     * @return List<CommonItem>
     */
    public List<CommonItem> getList() {
        return mList;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public CommonItem getItem(int i1) {
        return mList != null ? mList.get(i1) : null;
    }

    @Override
    public long getItemId(int i1) {
        return i1;
    }

    @Override
    public Component getComponent(int i1, Component component, ComponentContainer componentContainer) {
        Component resultComponent = null;
        CommonItem item = getItem(i1);
        int type = getItemComponentType(i1);

        if (component == null) {
            switch (type) {
                case CommonItem.TYPE_MENU_TITLE:
                    ViewHolderMenuTitle holderMenuTitle = new ViewHolderMenuTitle();
                    resultComponent = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_list_item_menu_title, componentContainer, false);
                    holderMenuTitle.textView = (Text) resultComponent.findComponentById(ResourceTable.Id_text_menuTitleItem_title);
                    resultComponent.setTag(holderMenuTitle);
                    break;
                case CommonItem.TYPE_CHECK_MENU:
                    ViewHolderCheckMenu holderCheckMenu = new ViewHolderCheckMenu();
                    resultComponent = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_list_item_check_box_menu, componentContainer, false);
                    holderCheckMenu.textView = (Text) resultComponent.findComponentById(ResourceTable.Id_text_checkBoxMenuItem);
                    holderCheckMenu.checkbox = (Checkbox) resultComponent.findComponentById(ResourceTable.Id_checkBox_checkBoxMenuItem);
                    resultComponent.setTag(holderCheckMenu);
                    break;
                case CommonItem.TYPE_INFO_MENU:
                    ViewHolderInfoMenu holderInfoMenu = new ViewHolderInfoMenu();
                    resultComponent = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_list_item_info_menu, componentContainer, false);
                    holderInfoMenu.titleTextView = (Text) resultComponent.findComponentById(ResourceTable.Id_text_infoMenuItem_title);
                    holderInfoMenu.infoTextView = (Text) resultComponent.findComponentById(ResourceTable.Id_text_infoMenuItem_info);
                    resultComponent.setTag(holderInfoMenu);
                    break;
                default:
                    break;
            }
        } else {
            resultComponent = component;
        }

        switch (type) {
            case CommonItem.TYPE_MENU_TITLE:
                handleMenuTitle(item, resultComponent);
                break;
            case CommonItem.TYPE_CHECK_MENU:
                handleCheckMenu(item, resultComponent);
                break;
            case CommonItem.TYPE_INFO_MENU:
                handleInfoMenu(item, resultComponent);
                break;
            default:
                break;
        }
        return resultComponent;
    }

    private void handleMenuTitle(CommonItem item, Component resultComponent) {
        MenuTitle menuTitle = (MenuTitle) item;
        ViewHolderMenuTitle holderMenuTitle = (ViewHolderMenuTitle) resultComponent.getTag();
        holderMenuTitle.textView.setText(menuTitle.title);
    }

    private void handleCheckMenu(CommonItem item, Component resultComponent) {
        CheckMenu checkMenu = (CheckMenu) item;
        ViewHolderCheckMenu holderCheckMenu = (ViewHolderCheckMenu) resultComponent.getTag();
        holderCheckMenu.textView.setText(checkMenu.title);
        holderCheckMenu.checkbox.setChecked(checkMenu.isChecked());

        Component.ClickedListener clickedListener = new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                component.setBackground(new ShapeElement(mContext, ResourceTable.Graphic_shape_item_pressed));
                mContext.getUITaskDispatcher().delayDispatch(() -> {
                    component.setBackground(new ShapeElement(mContext, ResourceTable.Graphic_shape_item_normal));
                    checkMenu.onClick(SettingsItemProvider.this);
                }, 100);
            }
        };

        resultComponent.setClickedListener(clickedListener);
        holderCheckMenu.checkbox.setClickedListener(clickedListener);
    }

    private void handleInfoMenu(CommonItem item, Component resultComponent) {
        InfoMenu infoMenu = (InfoMenu) item;
        ViewHolderInfoMenu holderInfoMenu = (ViewHolderInfoMenu) resultComponent.getTag();
        holderInfoMenu.titleTextView.setText(infoMenu.title);
        holderInfoMenu.infoTextView.setText(infoMenu.getInfo());

        resultComponent.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                component.setBackground(new ShapeElement(mContext, ResourceTable.Graphic_shape_item_pressed));
                mContext.getUITaskDispatcher().delayDispatch(() -> {
                    component.setBackground(new ShapeElement(mContext, ResourceTable.Graphic_shape_item_normal));
                    infoMenu.onClick(SettingsItemProvider.this);
                }, 100);
            }
        });
    }

    @Override
    public int getItemComponentType(int position) {
        return getItem(position).type;
    }

    @Override
    public int getComponentTypeCount() {
        return 3;
    }

    private static class ViewHolderMenuTitle {
        private Text textView;
    }

    private static class ViewHolderCheckMenu {
        private Text textView;
        private Checkbox checkbox;
    }

    private static class ViewHolderInfoMenu {
        private Text titleTextView;
        private Text infoTextView;
    }
}
